/*
* 方量设置
* */

import 'package:center_beijing/api/product_api.dart';
import 'package:center_beijing/common/numLengthInputFormatter.dart';
import 'package:center_beijing/common/styles.dart';
import 'package:center_beijing/widgets/custom_input_widget.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:get/get.dart';

class SquareQuantity extends StatefulWidget {
  @override
  _SquareQuantityState createState() => _SquareQuantityState();
}

class _SquareQuantityState extends State<SquareQuantity> {
  List comList = Get.arguments;
  var textInput = TextEditingController();

  submit() async {
    var pass = {
      'compIds': this.comList.map((e) {
        return e['id'];
      }).toList(),
      'concreteUsage': double.parse(textInput.text) / this.comList.length
    };
    var result = await addConcreteUsage(pass);

    if (result != null) {
      Fluttertoast.showToast(msg: '配置成功');
      Get.back(result: 'ok');
    } else {}
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text('方量设置'),
        ),
        body: SafeArea(
          child: Container(
            padding: EdgeInsets.fromLTRB(0, 0, 0, 70),
            child: SingleChildScrollView(
              child: Column(children: [
                Container(
                  margin: EdgeInsets.fromLTRB(0, Styles.spacing8, 0, 0),
                  padding: EdgeInsets.fromLTRB(
                      Styles.spacing8, 0, Styles.spacing8, 0),
                  child: CustomInput(
                    textAlign: TextAlign.left,
                    keyboardType:
                    TextInputType.numberWithOptions(decimal: true),
                    inputformat: [
                      LengthLimitingTextInputFormatter(10),
                      NumLengthInputFormatter(digit: 2),
                    ],
                    title: '方量(m³)',
                    placeHold: '请输入',
                    textInput: this.textInput,
                    clearAll: false,
                    require: false,
                  ),
                ),
              ]),
            ),
          ),
        ),
        floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
        floatingActionButton: Container(
          padding: EdgeInsets.fromLTRB(
              Styles.spacing8, Styles.spacing8, Styles.spacing8, 10),
          child: Container(
            decoration: new BoxDecoration(
              //背景
              color: Styles.BlueColor,
              //设置四周圆角 角度
              borderRadius: BorderRadius.all(Radius.circular(12.0)),
            ),
            child: FlatButton(
              disabledColor: Styles.SelectColor,
              onPressed: () {
                FocusScope.of(context).requestFocus(new FocusNode());
                if (textInput.text == '') {
                  Fluttertoast.showToast(msg: '请输入方量');
                } else {
                  showDialog(
                      barrierDismissible: false,
                      context: this.context,
                      builder: (BuildContext context) {
                        return AlertDialog(
                          title: Text('注意'),
                          content: Text('确认提交吗？'),
                          actions: <Widget>[
                            new Container(
                              child: FlatButton(
                                child: new Text(
                                  "取消",
                                  style: TextStyle(color: Colors.black),
                                ),
                                onPressed: () {
                                  Navigator.pop(context);
                                },
                              ),
                            ),
                            new Container(
                              child: FlatButton(
                                child: new Text(
                                  "确定",
                                  style: TextStyle(color: Colors.red),
                                ),
                                onPressed: () {
                                  Navigator.pop(context);
                                  this.submit();
                                },
                              ),
                            ),
                          ],
                        );
                      });
                }
              },
              child: Container(
                height: Styles.MainButtonHeight,
                alignment: Alignment.center,
                child: Text(
                  '确定',
                  style: TextStyle(
                      fontSize: Styles.MainButtonTextFont, color: Colors.white),
                ),
              ),
            ),
          ),
        ));
  }
}
